﻿using System;
using UnityEngine;
using UnityEngine.EventSystems;

/*
 * 采用接口方式，不使用继承EventTrigger的方式
 * 原因：继承EventTrigger，会导致UGUI原生实现了EventTrigger的UI自身事件失效，也就是与EventTrigger
 * 冲突了，例如ScrollView无法滚动
 */

public class EventTriggerListener : MonoBehaviour, IPointerClickHandler,IPointerDownHandler
    ,IPointerUpHandler,IPointerEnterHandler,IPointerExitHandler,ISelectHandler
    ,IDeselectHandler,IUpdateSelectedHandler
{
    public delegate void VoidDelegate(GameObject go);
    public delegate void BoolDelegate(GameObject go, bool state);
    public delegate void FloatDelegate(GameObject go, float delta);
    public delegate void VectorDelegate(GameObject go, Vector2 delta);
    public delegate void ObjectDelegate(GameObject go, object  userData);
    public delegate void KeyCodeDelegate(GameObject go, KeyCode key);

    public VoidDelegate onClick;
    public VoidDelegate onClick_DontTriggerDouble; //双击时不触发单击的单击事件
    public VoidDelegate onDoubleClick;
    public VoidDelegate onDown;
    public VoidDelegate onEnter;
    public VoidDelegate onExit;
    public VoidDelegate onUp;
    public VoidDelegate onSelect;
    public VoidDelegate onDeselect;
    public VoidDelegate onUpdateSelect;

    public ObjectDelegate onClick_Obj;
    public object userData;
    private bool isDoubleClick = false; //单独处理，同时含有单击和双击事件的按钮

    static public EventTriggerListener Get(GameObject go)
    {
        EventTriggerListener listener = go.GetComponent<EventTriggerListener>();
        if (listener == null) listener = go.AddComponent<EventTriggerListener>();
        return listener;
    }

    static public EventTriggerListener Get(Transform transform)
    {
        EventTriggerListener listener = transform.GetComponent<EventTriggerListener>();
        if (listener == null) listener = transform.gameObject.AddComponent<EventTriggerListener>();
        return listener;
    }

    private void Update()
    {        
    }


    public void OnPointerClick(PointerEventData eventData)
    {
        if (eventData.button == PointerEventData.InputButton.Left)
        {
            if (onClick != null && eventData.clickCount == 1)
            {
                onClick(gameObject);
            }
            //if (onDoubleClick != null && eventData.clickCount == 2)
            //{
            //    onDoubleClick(gameObject);
            //}
            if (onClick_Obj != null && eventData.clickCount == 1)
            {
                onClick_Obj(gameObject, userData);
            }

            if (onClick_DontTriggerDouble != null && eventData.clickCount == 1)
            {
                Invoke("Delay_onClick_DontTriggerDouble", 0.3f); //延迟0.3s执行，判断这段时间是否再次点击
            }
            if (onDoubleClick != null && eventData.clickCount == 2)
            {
                onDoubleClick(gameObject);
                isDoubleClick = true;
            }
            else
            {
                isDoubleClick = false;
            }
        }
    }

    public void OnPointerExit(PointerEventData eventData)
    {
        if (onExit != null) onExit(gameObject);
    }

    public void OnPointerEnter(PointerEventData eventData)
    {
        if(onEnter != null) onEnter(gameObject);
    }

    public void OnPointerUp(PointerEventData eventData)
    {
        if (onUp != null && eventData.button == PointerEventData.InputButton.Left)
        {
            onUp(gameObject);
        }
    }

    public void OnPointerDown(PointerEventData eventData)
    {
        if (onDown != null && eventData.button == PointerEventData.InputButton.Left)
        {
            onDown(gameObject);
        }
    }

    public void OnSelect(BaseEventData eventData)
    {
        if (onSelect != null) onSelect(gameObject);
    }

    public void OnDeselect(BaseEventData eventData)
    {
        if (onDeselect != null) onDeselect(gameObject);
    }

    public void OnUpdateSelected(BaseEventData eventData)
    {
        if (onUpdateSelect != null) onUpdateSelect(gameObject);
    }
    /// <summary>
    /// 延迟调用，若0.3s内再次点击，则不执行此单击事件
    /// </summary>
    private void Delay_onClick_DontTriggerDouble()
    {
        if (!isDoubleClick)
        {
            onClick_DontTriggerDouble(gameObject);
        }
    }
}